home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / s-tasmem.adb < prev    next >
Text File  |  1996-01-30  |  4KB  |  102 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                 GNU ADA RUNTIME LIBRARY (GNARL) COMPONENTS               --
  4. --                                                                          --
  5. --                    S Y S T E M . T A S K _ M E M O R Y                   --
  6. --                                                                          --
  7. --                                  B o d y                                 --
  8. --                                                                          --
  9. --                             $Revision: 1.9 $                             --
  10. --                                                                          --
  11. --       Copyright (c) 1991,1992,1993,1994, FSU, All Rights Reserved        --
  12. --                                                                          --
  13. -- GNARL is free software; you can redistribute it  and/or modify it  under --
  14. -- terms  of  the  GNU  Library General Public License  as published by the --
  15. -- Free Software  Foundation;  either version 2, or (at  your  option)  any --
  16. -- later  version.  GNARL is distributed  in the hope that  it will be use- --
  17. -- ful, but but WITHOUT ANY WARRANTY;  without even the implied warranty of --
  18. -- MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Gen- --
  19. -- eral Library Public License  for more details.  You should have received --
  20. -- a  copy of the GNU Library General Public License along with GNARL;  see --
  21. -- file COPYING.LIB.  If not,  write to the  Free Software Foundation,  675 --
  22. -- Mass Ave, Cambridge, MA 02139, USA.                                      --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.Task_Primitives;
  27. --  Used for, Lock
  28. --            Unlock
  29. --            Initialize_Lock
  30. --            Write_Lock
  31.  
  32. package body System.Task_Memory is
  33.  
  34.    --  malloc() and free() are not currently thread-safe, though they should
  35.    --  be. In the meantime, these protected versions are provided.
  36.  
  37.    Memory_Mutex : Task_Primitives.Lock;
  38.  
  39.    --------------------
  40.    -- Low_Level_Free --
  41.    --------------------
  42.  
  43.    procedure Low_Level_Free (A : System.Address) is
  44.  
  45.       Error : Boolean;
  46.  
  47.       procedure free (Addr : System.Address);
  48.       pragma Import (C, free, "free");
  49.  
  50.    begin
  51.       Task_Primitives.Write_Lock (Memory_Mutex, Error);
  52.       free (A);
  53.       Task_Primitives.Unlock (Memory_Mutex);
  54.    end Low_Level_Free;
  55.  
  56.    -------------------
  57.    -- Low_Level_New --
  58.    -------------------
  59.  
  60.    function Low_Level_New
  61.      (Size : Storage_Elements.Storage_Count)
  62.       return System.Address
  63.    is
  64.       Temp : System.Address;
  65.       Error : Boolean;
  66.  
  67.       function malloc
  68.         (Size : in Storage_Elements.Storage_Count)
  69.          return System.Address;
  70.       pragma Import (C, malloc, "malloc");
  71.  
  72.    begin
  73.       Task_Primitives.Write_Lock (Memory_Mutex, Error);
  74.       Temp := malloc (Size);
  75.       Task_Primitives.Unlock (Memory_Mutex);
  76.       return Temp;
  77.    end Low_Level_New;
  78.  
  79.    --------------------------
  80.    -- Unsafe_Low_Level_New --
  81.    --------------------------
  82.  
  83.    function Unsafe_Low_Level_New
  84.      (Size : Storage_Elements.Storage_Count)
  85.       return System.Address
  86.    is
  87.       function malloc
  88.         (Size : in Storage_Elements.Storage_Count)
  89.          return System.Address;
  90.       pragma Import (C, malloc, "malloc");
  91.  
  92.    begin
  93.       return malloc (Size);
  94.    end Unsafe_Low_Level_New;
  95.  
  96. begin
  97.  
  98.    Task_Primitives.Initialize_Lock (Priority'Last, Memory_Mutex);
  99.    --  Initialize the lock used to synchronize low-level memory allocation.
  100.  
  101. end System.Task_Memory;
  102.